<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$this->model('model')->checkuser();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['hdps']=$basic['hdps'];
if($_RQ['op']=='main'){
	if($_RQ['lessonid']){
		$this->session('lessonid',intval($_RQ['lessonid']));
		$return['lessonid']=intval($_RQ['lessonid']);
	}
	$return['isyuyue']=intval($_RQ['isyuyue']);
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'storeid >'=>0);
	$return['bodylist']=pdo_getall('jy_edu_body',$params);
	$return['post']=array('bodyid'=>$return['bodylist'][0]['id']);
	set_html('title','校区列表');
	$return['sharedata']=array(
		'title'=>'校区列表'.$basic['share_title'],
		'desc'=>'校区列表'.$basic['share_desc'],
		'link'=>custom_url('jy_edu/store_list/',true),
		'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	exi($return);
}
if($_RQ['op']=='getlist'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	$lat_lng=$this->session('lat_lng');	
	$pindex = max(1, 1);
	$psize = 500;
	$total='';
	$fields=array('id','logo','title','address','mobile','worktime','lat_lng','pcd');
	$list=pdo_getslice('jy_edu_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as $k=>&$v){
		$v['pcd']=json_decode($v['pcd'],true);
		if(trim($_RQ['province']) && $v['pcd']['province']!=trim($_RQ['province'])){
			unset($list[$k]);
			continue;
		}
		if(trim($_RQ['city']) && $v['pcd']['city']!=trim($_RQ['city'])){
			unset($list[$k]);
			continue;
		}
		if(trim($_RQ['district']) && $v['pcd']['district']!=trim($_RQ['district'])){
			unset($list[$k]);
			continue;
		}
		$v['lat_lng']=json_decode($v['lat_lng'],true);
		$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
		$v['juli']=intval($v['juli']/1000);
		$v['worktime']=json_decode($v['worktime'],true);
		if($v['juli']<10){
			$v['peisongtime']=$v['juli']*8+38;
		}else if($v['juli']<30){
			$v['peisongtime']=$v['juli']*3+38;
		}else{
			$v['peisongtime']=$v['juli']*1+38;
		}
	}
	paixu($list,'juli');
	if($_RQ['limit']){
		$list=array_slice($list,0,intval($_RQ['limit']));
	}else{
		$page=intval($_RQ['page'])?:1;
		$start=($page-1)*5;
		$list=array_slice($list,$start,5);
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='getclass'){
	$bodyid=intval($_RQ['bodyid']);
	$classids=pdo_getall('jy_edu_order',array('status'=>array(1,2),'ispay'=>1,'bodyid'=>$bodyid,'uniacid'=>$_SESSION['uniacid']),'classid','classid');
	//print_r($classids);exit;
	if(!empty($classids)){
		$classids=array_keys($classids);	
		$params=array('uniacid'=>$_SESSION['uniacid'],'id'=>$classids);
		$list=pdo_getall('jy_edu_class',$params,'','','id desc');
	}
	foreach($list as $k=>&$v){
		$v['store']=pdo_get('jy_edu_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['storeid']));
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='o_detail'){
	if(!$_RQ['id']){
		exi('没有找到该校区，请重新选择！','error');
	}
	$opus=pdo_get('jy_edu_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//进入校区详情，记录访问者浏览信息
	$islook=pdo_get('jy_edu_looklist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(empty($islook)){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oid'=>$_RQ['id'],
			'mid'=>$_SESSION['uid'],
			'createtime'=>TIMESTAMP
		
		);
		pdo_insert('jy_edu_looklist',$insert);
	}
	$opus['createtime']=date('Y-m-d H:i:s',$opus['createtime']);
	$return['opus']=$opus;
	exi($return);
}
if($_RQ['op']=='like'){
	if(!$_RQ['id']){
		exi('没有找到该校区，请重新选择！','error');
	}
	$like=$_RQ['like'];
	$opus=pdo_get('jy_edu_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if($like==1){
		pdo_update('jy_edu_opus',array('like'=>$opus['like']+1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		pdo_update('jy_edu_looklist',array('islike'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		
	}else{
		pdo_update('jy_edu_opus',array('like'=>$opus['like']-1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		pdo_update('jy_edu_looklist',array('islike'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
}	
